package com.huajx.cseek.face;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.IntBuffer;

import com.ha.sdk.inner.ComHaSdkLibrary;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QnyfUpload {

    private Configuration config = new Configuration(Zone.autoZone());

    private String sk = "2ppIX13CZjoAIVob2GAmj6fq7OzQHH4DksOzt6Fr";

    private String ak = "jOa2p2C7ErGN342zbzEjmpTJDeEdly4f1A3p_Ab7";

    private String bucket_name = "studentface";//空间名称

    private String token;

    private String bucketPath = "http://huajx-face.7n.52ycm.com/";

    public QnyfUpload(String ak, String sk, String bucket_name, String bucketPath) {
        if (ak != null && ak != "") {
            this.ak = ak;
        }
        if (sk != null && sk != "") {
            this.sk = sk;
        }
        if (bucket_name != null && bucket_name != "") {
            this.bucket_name = bucket_name;
        }
        if (bucketPath != null && bucketPath != "") {
            this.bucketPath = bucketPath;
        }
        Auth auth = Auth.create(this.ak, this.sk);
        this.token = auth.uploadToken(this.bucket_name);
    }

    /**
     * 七牛云文件上传
     *
     * @param data     数据流
     * @param fileName
     * @return
     * @throws QiniuException
     */
    public String upload(byte[] data, String fileName) throws QiniuException {
        UploadManager uploadManager = new UploadManager(config);
        if (data == null || data.length == 0 && fileName == null) {
            return null;
        }
        Response res = uploadManager.put(data, fileName, token);
        return bucketPath + fileName;
    }
    /**
     * 空间文件删除
     * @param bucket_name
     * @param key
     * @throws QiniuException
     */
    public void delete(String bucket_name, String key) throws QiniuException {
        Auth auth = Auth.create(this.ak, this.sk);
        BucketManager bucketManager = new BucketManager(auth, config);
        bucketManager.delete(bucket_name, key);
    }
    /**
     * 读取网络图片
     * @param path 文件路径
     * @return
     * @throws IOException
     */
    public static byte[] readFile(String path) throws IOException {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        DataInputStream in = new DataInputStream(conn.getInputStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }

        return out.toByteArray();
    }
    
    
    
    public static Object[] twistImage(byte[] imageData){
    	byte[] twist_image = new byte[1024 * 1024];
        byte[] thumb_image = new byte[1024 * 1024];
        IntBuffer twist_size = IntBuffer.allocate(1);
        IntBuffer thumb_size = IntBuffer.allocate(1);
		IntBuffer feature_size = IntBuffer.allocate(1);
		IntBuffer faceJpgLen = IntBuffer.allocate(1);
		IntBuffer twist_w = IntBuffer.allocate(1);
		IntBuffer twist_h = IntBuffer.allocate(1);        
        int _ret =  ComHaSdkLibrary.INSTANCE.HA_GetJpgFeatureImage(imageData, imageData.length, twist_image, twist_size, twist_w, twist_h, thumb_image, thumb_size);
        if (_ret != 0)
			return new Object[] { _ret };
        byte[] faceImg = new byte[thumb_size.get()];
        byte[] twistImg = new byte[twist_size.get()];
        System.arraycopy(thumb_image, 0, faceImg, 0, faceImg.length);
        System.arraycopy(twist_image, 0, twistImg, 0, twistImg.length);
		return new Object[] { 0, faceImg, twistImg };
    }

    public String getSk() {
        return sk;
    }

    public void setSk(String sk) {
        this.sk = sk;
    }

    public String getAk() {
        return ak;
    }

    public void setAk(String ak) {
        this.ak = ak;
    }

    public String getBucket_name() {
        return bucket_name;
    }

    public void setBucket_name(String bucket_name) {
        this.bucket_name = bucket_name;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
